<template>
  <div>
    <h1>Items</h1>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}: {{ item.description }}</li>
    </ul>
    <h2>Add Item</h2>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      newItem: {
        name: '',
        description: ''
      }
    }
  },
  created() {
    this.fetchItems()
  },
  methods: {
    fetchItems() {
      this.$axios.get('/items')
        .then(response => {
          this.items = response.data
        })
        .catch(error => {
          console.error('Error fetching items:', error)
        })
    },
    addItem() {
      this.$axios.post('/items', this.newItem)
        .then(response => {
          this.items.push(response.data)
          this.newItem.name = ''
          this.newItem.description = ''
        })
        .catch(error => {
          console.error('Error adding item:', error)
        })
    }
  }
}
</script>

<style scoped>
/* 添加样式 */
</style>